package com.jojo.android.zxlib.view.dialog;

import android.app.Dialog;
import android.content.Context;

import androidx.annotation.ColorInt;

import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jojo.android.zxlib.R;
import com.jojo.android.zxlib.view.spinkit.SpinKitView;
import com.jojo.android.zxlib.view.spinkit.SpriteFactory;
import com.jojo.android.zxlib.view.spinkit.Style;


public class LoadingDialog implements ILoading {
    private Dialog dialog;
    private SpinKitView spinKitView;
    private TextView tips_tv;
    private Context context;

    public LoadingDialog(Context context) {
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null, false);
        spinKitView = view.findViewById(R.id.spinKitView);
        tips_tv = view.findViewById(R.id.tips_tv);
        dialog = new Dialog(context, R.style.mDialog);
        dialog.setContentView(view,
                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        dialog.setCanceledOnTouchOutside(false);
        dialog.getWindow().getAttributes().dimAmount = 0.2F;
        dialog.setCancelable(true);
    }

    public void setStyle(Style style) {
        if (spinKitView != null) {
            spinKitView.setIndeterminateDrawable(SpriteFactory.create(style));
        }
    }

    public void setCancelable(boolean cancelable) {
        if (dialog != null) {
            dialog.setCancelable(cancelable);
        }
    }

    @Override
    public void setLoadingCancelListener(final LoadingCancelListener listener) {
        if (dialog != null)
            dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    if (listener != null) {
                        listener.onCancelLoading();
                    }
                }
            });
    }

    public void setColor(@ColorInt int color) {
        if (spinKitView != null) {
            spinKitView.setColor(color);
        }
    }

    public void setTipsColor(@ColorInt int color) {
        tips_tv.setTextColor(color);
    }

    public void setTipsText(CharSequence text) {
        tips_tv.setText(text);
    }


    public void show() {
        if (!dialog.isShowing()) {
            dialog.show();
        }
    }

    public void cancel() {
        if (dialog.isShowing()) {
            dialog.cancel();
        }
    }

    @Override
    public boolean isLoading() {
        return false;
    }

}
